<template>
  <div id="setEmployPuthorization">
    <el-form
      ref="authFormRef"
      :model="authForm"
      :rules="authFormRule"
      label-width="120px"
      class="form-input-m editAutCode"
      :inline-message="true"
    >
      <el-form-item prop="hashedCode" label="授权码">
        <el-input
          type="text"
          v-model="authForm.hashedCode"
          placeholder="请输入授权码"
          show-password
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer1 flex justify-center padding-sm">
      <el-button type="primary" @click="saveAuth('authFormRef')">保存</el-button>
      <el-button @click="closeAuthDialog('authFormRef')">关闭</el-button>
    </div>
  </div>
</template>
<script lang="ts" setup>
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance } from 'vue';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';

// 全局变量定义区 ==========
let authForm = reactive({
  id: '',
  hashedCode: ''
});
let authFormRule = {
  hashedCode: [
    {
      required: true,
      message: '请输入授权码',
      trigger: 'blur'
    }
  ]
};

//暴露属性和方法
defineExpose({
  init
});

// 方法定义区 =============
function init(item: any) {
  // console.log('父组件机构给授权码子组件传过来的值', item);

  // 如果item.id员工id存在说明是编辑赋值 否则就是新建
  if (item.id) {
    authForm.id = item.id; //员工id
  } else {
    authForm.id = '';
  }
}

//保存
function saveAuth(formName: any) {
  const $el = proxy.$refs.authFormRef;
  $el.validate((valid: any) => {
    if (!valid) {
      return false;
    }
    //保存授权码
    const hashedCode = authForm.hashedCode;
    const authUrl =
      '/api/sys/hisEmployee/updateEmpowerNum/' + authForm.id + '/' + authForm.hashedCode;
    proxy.$http
      .put(authUrl)
      .then((res: any) => {
        VxeMessage('修改授权码成功', VxeNotifyType.success);
      })
      .catch((err: any) => {
        VxeMessage('修改授权码失败', VxeNotifyType.error);
      });
  });
}

// @Emit('closePerDialog')
function closeAuthDialog(formName: any) {
  const el: any = proxy.$refs[formName];
  //移除校验结果
  el.clearValidate();
  authForm.id = '';
  authForm.hashedCode = '';
  proxy.$emit('closePerDialog');
}
</script>
<style lang="scss" scoped>
.editAutCode {
  margin-top: 10px;
}
</style>
